package com.ruoyi.client.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.client.domain.entity.BlogArticle;
import com.ruoyi.client.domain.entity.BlogCategory;
import com.ruoyi.client.mapper.BlogArticleMapper;
import com.ruoyi.client.mapper.BlogCategoryMapper;
import com.ruoyi.client.service.IBlogCategoryService;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 分类表 服务实现类
 * </p>
 *
 * @author fz
 * @since 2025-02-27
 */
@Service
public class BlogCategoryServiceImpl extends ServiceImpl<BlogCategoryMapper, BlogCategory> implements IBlogCategoryService {
    @Autowired
    BlogCategoryMapper categoryMapper;
    @Autowired
    BlogArticleMapper articleMapper;
    @Override
    public TableDataInfo<BlogCategory> selectBlogCategoryList(Map<String, Object> params, PageQuery pageQuery) {
        Page<BlogCategory> page = this.page(pageQuery.build(), buildWrapper(params));
        return TableDataInfo.build(page);
    }

    @Override
    public void deleteArticleCategoryByIds(Long[] categoryIds) {
        for (Long categoryId : categoryIds) {
            BlogCategory blogCategory = getById(categoryId);
            if (articleMapper.exists(Wrappers.<BlogArticle>lambdaQuery().eq(BlogArticle::getArticleType,blogCategory.getArticleType()))) {
                throw new ServiceException(String.format("%1$s已分配，不能删除",blogCategory.getName()));
            }
        }
        this.removeBatchByIds(Arrays.asList(categoryIds));
    }


    private Wrapper<BlogCategory> buildWrapper(Map<String, Object> params) {
        QueryWrapper<BlogCategory> wrapper = new QueryWrapper<>();
        if (CollectionUtils.isEmpty(params)) return wrapper;
        String beginTime = (String) params.get("beginTime");
        String endTime = (String) params.get("endTime");
        if (StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(endTime)){
            wrapper.between("create_time",beginTime,endTime);
            params.remove("beginTime");
            params.remove("endTime");
        }
        Set<Map.Entry<String, Object>> set = params.entrySet();
        for (Map.Entry<String, Object> entry : set) {
            String field = entry.getKey();
            Object value = entry.getValue();

            if (value != null) {
                // 添加等值条件
                wrapper.like(field,value);
            }
        }
        return wrapper;
    }
}
